package br.urca.interceptor;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import br.com.caelum.vraptor.Accepts;
import br.com.caelum.vraptor.AroundCall;
import br.com.caelum.vraptor.Intercepts;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.controller.ControllerMethod;
import br.com.caelum.vraptor.controller.IndexController;
import br.com.caelum.vraptor.interceptor.SimpleInterceptorStack;
import br.urca.controller.RelatorioHistoricoController;

@Intercepts
public class RelatorioHistoricoInterceptor {
	
	private Result result;
	private HttpServletRequest request;
	
	@Inject
	public RelatorioHistoricoInterceptor(Result result, 
			HttpServletRequest request) {
		this.result = result;
		this.request = request;
	}
	
	@Deprecated RelatorioHistoricoInterceptor() {}
	
	@AroundCall
	public void valida(SimpleInterceptorStack stack) {
		if (urlValida(request.getParameter("comGrade"))) {
			stack.next();
		} else {
			result
				.include("mensagem", "Atenção! Parâmetro inválido!")
				.redirectTo(IndexController.class).index();
		}
	}
	
	private boolean urlValida(String parametro) {
		try {
			return /*(parametro.equals("true") ||*/ parametro.equals("false");
		} catch (Exception e) {
			return false;
		}
	}

	@Accepts
	public boolean accepts(ControllerMethod method) {
		return method.getController().getType().equals(RelatorioHistoricoController.class) &&
				method.getMethod().getName().equals("historico");
	}

}
